package io.adminboot.utils;


import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    private final static int SUCCESS_CODE = 1;
    private final static int ALERT_CODE = 0;

    public R() {
        put("code", SUCCESS_CODE);
        put("msg", "success");
    }

    public static R alert(String alertMsg) {
        R r = new R();
        r.put("code", ALERT_CODE);
        r.put("msg", alertMsg);
        return r;
    }

    public static R error() {
        return error(ApiStatusEnum.SC_INTERNAL_SERVER_ERROR);
    }

    public static R error(ApiStatusEnum status) {
        R r = new R();
        r.put("code", status.getCode());
        r.put("msg", status.getMsg());
        return r;
    }

    public static R error(ApiStatusEnum status, String debugMsg) {
        R r = new R();
        r.put("code", status.getCode());
        r.put("msg", status.getMsg());
        r.put("debugMsg", debugMsg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
